﻿/****************************************/
/***************  CALL 1  ***************/
function GetAllArticleArray() {
    //alert("Calling method");
    PageMethods.GetAllArticles(onSuccessGetAllArticleArray, onCallError);
}
function onSuccessGetAllArticleArray(response) {
    //alert("-GetAllArticles- called successfully !");    
    $("#resultList").html(format3Table(response));
    setEvents();
}

/***************  CALL 2  ***************/
function GetPagedArticleArray() {
    PageMethods.GetPagedArticles(onSuccessGetPagedArticles, onCallError);
}
function onSuccessGetPagedArticles(response) {
    //alert("-GetPagedArticles- called successfully !");    
    $("#resultList").html(format3Table(response));
    setEvents();
}

/***************  CALL 3  ***************/
function GetPageOfArticles() {
    var pageNum = document.getElementById('textBoxPageNumber').value;
    var pageSize = document.getElementById('textBoxPageSize').value;

    PageMethods.GetPageOfArticles(pageNum, pageSize, onSuccessGetPagedArticles, onCallError);
}
function onSuccessGetPageOfArticles(response) {
    //alert("-GetPagedArticles- called successfully !");    
    $("#resultList").html(format3Table(response));
    setEvents();
}

/***************  CALL Errors  ***************/
function onCallError(error) {
    alert("Stack Trace: " + error.get_stackTrace() + "; " +
        "Error: " + error.get_message() + "; " +
        "Status Code: " + error.get_statusCode() + "; " +
        "Exception Type: " + error.get_exceptionType() + "; " +
        "Timed Out: " + error.get_timedOut());
}

/******************************************/
/***************  FORMAT 1  ***************/
function formatTable(s) {
    var result = "<table>";
    for (var i in s) {
        //alert(s[i].logo + " " + s[i].weblink);
        result += "<tr><td>" + s[i].logo + "</td><td>";
        result += s[i].weblink + "</td></tr>";
    }
    result += "</table>";
    return result;
}

/***************  FORMAT 2  ***************/
function format2Table(s) {
    var result = "<table>";
    for (var i in s) {
        //alert(s[i].logo + " " + s[i].weblink);
        //header row
        result += "<tr class=\"articleHead\"><td>" + s[i].logo + "</td>";
        result += "<td>" + s[i].title + "</td>";
        result += "<td>" + s[i].pubdate + "</td></tr>";
        //content row
        result += "<tr class=\"articleContent\"><td colspan=\"3\">";
        result += "<div class=\'description\'>" + s[i].description + "</div>";
        result += "<div class=\'link\'>" + s[i].weblink + "</div>";
        result += "</td></tr>";
    }
    result += "</table>";
    return result;
}

/***************  FORMAT 3  ***************/
function format3Table(s) {
    var result = "";
    result += "<div class=\'articlesTableHead\'>";
    result += "<table><tr><td>Logo</td>";
    result += "<td> Categorie </td>";
    result += "<td> Titlu </td>";
    result += "<td> Data publicarii </td></tr></table>";
    result += "</div>";
    for (var i in s) {
        //alert(s[i].logo + " " + s[i].weblink);
        //header row
        result += "<div class=\'articleHead\'>";
        result += "<table><tr><td><img src=\"" + s[i].logo + "\" alt=\"Logo"+ s[i].logo +"\" class=\"logoimage\"/></td>";
        result += "<td>" + s[i].category + "</td>";
        result += "<td>" + s[i].title + "</td>";
        result += "<td>" + s[i].pubdate.toISOString() + "</td></tr></table>";
        result += "</div>";
        //content row
        result += "<div class=\"articleContent\">";
        result += s[i].description + "<br/><a href=\"" + s[i].weblink + "\" target=\"_blank\">-- Articolul in original --</a>";
        result += " <br/></div>";
    }
    return result;
}

/*************************************************/
/***************  EVENTHANDLING 1  ***************/
function setEvents() {
    $('.articleHead').click(function () {
        //REMOVE THE ON CLASS FROM ALL BUTTONS
        $('.articleHead').removeClass('on');
        //NO MATTER WHAT WE CLOSE ALL OPEN SLIDES
        $('.articleContent').slideUp();
        //IF THE NEXT SLIDE WASN'T OPEN THEN OPEN IT
        if ($(this).next().is(':hidden') === true) {
            //ADD THE ON CLASS TO THE BUTTON
            $(this).addClass('on');
            //OPEN THE SLIDE
            $(this).next().slideDown();
        }
    });
    /*** REMOVE IF MOUSEOVER IS NOT REQUIRED ***/
    //ADDS THE .OVER CLASS FROM THE STYLESHEET ON MOUSEOVER 
    $('.articleHead').mouseover(function () {
        if (!$(this).hasClass('on'))
            $(this).addClass('over');
        //ON MOUSEOUT REMOVE THE OVER CLASS
    }).mouseout(function () {
        $(this).removeClass('over');
    });

    /*** END REMOVE IF MOUSEOVER IS NOT REQUIRED ***/
    $('.articleContent').hide();
}
